//
//  CollectViewController.m
//  Double
//
//  Created by dllo on 15/5/25.
//  Copyright (c) 2015年 袁庆秋. All rights reserved.
//

#import "CollectViewController.h"
#import "AnimationCollectDatabase.h"
#import "AnimationCollectModel.h"
#import "CartoonRecommendDetailViewController.h"
#import "ColletTableViewCell.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>

@interface CollectViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain)UITableView * tableView;
@property (nonatomic, retain)NSArray * tableArray;
@end

@implementation CollectViewController

- (void)dealloc
{
    [_tableArray release];
    [_tableView release];
    [super dealloc];
}
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.tableArray = [NSArray array];
    }
    return self;
}
- (void)loadView
{
    [super loadView];
    [self createView];
    
    self.navigationItem.title = @"我的收藏";
    
    //    self.navigationItem.titleView = self.segmented;
    self.navigationController.navigationBar.translucent = NO;
    //    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"fanhui.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftbuttonAction)];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
}
- (void)leftbuttonAction
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)createView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    [self.tableView release];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    self.tableArray = [AnimationCollectDatabase selectAll];
    if (self.tableArray.count == 0) {
        return @"暂无收藏记录";
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 145;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{
    NSUserDefaults *loginName = [NSUserDefaults standardUserDefaults];
    NSInteger login = [[loginName objectForKey:@"loginName"] integerValue];
    self.tableArray = [AnimationCollectDatabase selectUID:login];
    return self.tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellInde = @"cell";
    ColletTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellInde];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (!cell) {
        cell = [[[ColletTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellInde]autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
}
    NSUserDefaults *loginName = [NSUserDefaults standardUserDefaults];
    NSInteger login = [[loginName objectForKey:@"loginName"] integerValue];
    self.tableArray = [AnimationCollectDatabase selectUID:login];
    AnimationCollectModel *model1 = [self.tableArray objectAtIndex:indexPath.row];
    [cell.label1 setText:model1.AnimationName];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:model1.AnimationPic] placeholderImage:[UIImage imageNamed:@"placeholder.jpg"] options:SDWebImageRetryFailed];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.tableView deselectRowAtIndexPath:[self.tableView indexPathForSelectedRow] animated:YES];
    CartoonRecommendDetailViewController *cartoon = [[CartoonRecommendDetailViewController alloc] init];
    NSUserDefaults *loginName = [NSUserDefaults standardUserDefaults];
    NSInteger login = [[loginName objectForKey:@"loginName"] integerValue];
    self.tableArray = [AnimationCollectDatabase selectUID:login];
    AnimationCollectModel *model1 = [self.tableArray objectAtIndex:indexPath.row];
    cartoon.iD = [NSString stringWithFormat:@"%ld", model1.idmin];
    cartoon.titleName = model1.AnimationName;
    UIImageView * ImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:model1.AnimationPic]];
    ImageView.frame = CGRectMake(0, 0, 50, 50);
    [cartoon.view addSubview:ImageView];
    [self.navigationController pushViewController:cartoon animated:YES];
    [cartoon release];
    [ImageView release];
}
// 重写系统按钮触发的方法(让其处于编辑状态)
-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    [self.tableView setEditing:editing animated:animated];
}
// 编辑删除和插入
-(UITableViewCellEditingStyle) tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.tableArray.count) {
        return UITableViewCellEditingStyleInsert;
    }
    else {
        return UITableViewCellEditingStyleDelete;
    }
}
#pragma mark - 主要
// 删除对应数据
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        [self.tableArray removeObjectAtIndex:indexPath.row];
        NSUserDefaults *loginName = [NSUserDefaults standardUserDefaults];
        NSInteger login = [[loginName objectForKey:@"loginName"] integerValue];
//        CartoonRecommendDetailViewController *cartoon = [[CartoonRecommendDetailViewController alloc] init];
//        self.tableArray = [AnimationCollectDatabase selectID:[cartoon.iD integerValue] UID:login];
        self.tableArray = [AnimationCollectDatabase selectUID:login];
        AnimationCollectModel *model1 = [self.tableArray objectAtIndex:indexPath.row];

        [AnimationCollectDatabase deleteWithModel:model1];
    }
    [self.tableView reloadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
